package netty.discard;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

//目的：抛弃所有请求
//处理服务端 channel
public class DiscardServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //丢弃接收到的数据
        //ByteBuf 是一个引用计数对象，这个对象必须显示地调用 release() 方法来释放
        ByteBuf buf = (ByteBuf) msg;
        System.out.println(buf.toString(CharsetUtil.US_ASCII));
        buf.release();
        System.out.println("channelRead and release!!");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //不同异常可能有不同实现，此处暂且关闭连接
        cause.printStackTrace();
        ctx.close();
    }
}
